package com.jiesen.base.modules.common.dictionaries.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.modules.common.dictionaries.mode.Dictionaries;
import com.jiesen.base.modules.common.dictionaries.service.DictionariesService;
import com.jiesen.base.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//数据字典
@RestController
@RequestMapping("/dictionaries")
public class DictionariesController {

    @Autowired
    private DictionariesService dictionariesService;

    //新增一个数据字典字段
    @PostMapping("/create")
    public R create(@RequestBody Dictionaries dictionaries){
        boolean save = dictionariesService.save(dictionaries);
        if(!save){
            return R.error();
        }
        return R.ok();
    }

    //根据类型查询字典
    @GetMapping("/view")
    public R view(@RequestParam(value = "id",required = false,defaultValue = "1")String id){
        QueryWrapper<Dictionaries> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("`type`",id);
        List<Dictionaries> list = dictionariesService.list(queryWrapper);
        return R.ok().put("data",list);
    }

    @GetMapping("/view{type}")
    public R viewType(@PathVariable("type")String type){
        QueryWrapper<Dictionaries> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<Dictionaries> list = dictionariesService.list(queryWrapper);
        return R.ok().put("data",list);
    }

    //查询按钮的数据字典
    @GetMapping("/viewButton")
    public R viewButton(){
        return view("1");
    }

    //查询图表的数据字典
    @GetMapping("/viewIcon")
    public R viewIcon(){
        return view("2");
    }

    //根据条件查询数据字典
    @PostMapping("/view")
    public R view(@RequestBody Dictionaries dictionaries){
        return dictionariesService.page(dictionaries);
    }

    //查询数据字典的类型
    @GetMapping("/getList")
    public R viewList(@RequestParam(value = "parentId",defaultValue = "0")Integer id){
        QueryWrapper<Dictionaries> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Dictionaries> list = dictionariesService.list(queryWrapper);
        return R.ok().put("data",list);
    }

    //删除一个数据字典字段
    @GetMapping("/delete")
    public R delete(@RequestParam("id")Integer id){
        boolean remove = dictionariesService.removeById(id);
        if(!remove){
            return R.error();
        }
        return R.ok();
    }

    //修改一个数据字典字段
    @PostMapping("/edit")
    public R edit(@RequestBody Dictionaries dictionaries){
        boolean save = dictionariesService.updateById(dictionaries);
        if(!save){
            return R.error();
        }
        return R.ok();
    }
}